package com.example.himalaya.view.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.view.activity.PlayerViewActivity;
import com.example.himalaya.view.adapters.TrackListAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.view.callback.IHistoryViewCallback;
import com.example.himalaya.presenters.HistoryPresenter;
import com.example.himalaya.presenters.PlayerPresenter;
import com.example.himalaya.view_component.ConfirmCheckBoxDialog;
import com.example.himalaya.view_component.UILoader;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;
import java.util.Objects;

public class HistoryViewFragment extends BaseFragment implements IHistoryViewCallback {


    // Adapter
    private TrackListAdapter mTrackListAdapter;


    // Logic object
    private HistoryPresenter mHistoryPresenter;


    private Track mCurrentClickHistoryItem = null;

    // Other object
    private View mSuccessView = null;
    private UILoader mUiLoader = null;
    private RecyclerView mHistoryList;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, ViewGroup container) {

        if (mUiLoader == null) {

            mUiLoader = new UILoader(container.getContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSuccessView(layoutInflater, container);
                }

                @Override
                protected View getEmptyView() {

                    View itemView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);

                    TextView tipsView = itemView.findViewById(R.id.tips_for_empty);

                    tipsView.setText(R.string.tips_for_empty_history);
                    return itemView;
                }

                @Override
                protected View getNetworkErrorView() {
                    View itemView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_error_view, this, false);

                    TextView tipsView = itemView.findViewById(R.id.tips_for_error);

                    tipsView.setText(R.string.tips_for_error_history);
                    return itemView;
                }
            };
        } else {
            if (mUiLoader.getParent() instanceof ViewGroup) {
                ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
            }
        }


        return mUiLoader;
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {

        mSuccessView = layoutInflater.inflate(R.layout.main_history_container, container, false);

        initSuccessVariable();

        initSuccessAdapter();

        initPresenter();

        initListener();

        initView(container);

        return mSuccessView;
    }

    private void initSuccessAdapter() {
        // 设置适配器
        if (mTrackListAdapter == null) {
            mTrackListAdapter = new TrackListAdapter();
        }

        mHistoryList.setAdapter(mTrackListAdapter);
    }

    private void initSuccessVariable() {
        mHistoryList = mSuccessView.findViewById(R.id.history_list);
    }

    private void initListener() {
        mTrackListAdapter.setItemClickListener(new TrackListAdapter.ItemClickListener() {
            @Override
            public void onItemClick(List<Track> detailData, int position) {
                // 设置播放器的数据
                PlayerPresenter playerPresenter = PlayerPresenter.getPlayerPresenter();

                playerPresenter.setPlayList(detailData, position);

                // 跳转到播放器界面
                Intent intent = new Intent(Objects.requireNonNull(getActivity()), PlayerViewActivity.class);

                startActivity(intent);
            }
        });
        mTrackListAdapter.setItemLongClickListener(new TrackListAdapter.ItemLongClickListener() {
            @Override
            public void onItemLongClick(Track track) {
                // Toast.makeText(getContext(), "长按", Toast.LENGTH_LONG).show();

                mCurrentClickHistoryItem = track;

                ConfirmCheckBoxDialog dialog = new ConfirmCheckBoxDialog(Objects.requireNonNull(getActivity()));

                dialog.setonDialogActionClickListener(new ConfirmCheckBoxDialog.onDialogActionClickListener() {
                    @Override
                    public void onRightBtnClick() {

                    }

                    @Override
                    public void onLeftBtnClick(boolean checked) {
                        if (mHistoryPresenter != null) {
                            if (mCurrentClickHistoryItem != null) {
                                if (!checked) {
                                    mHistoryPresenter.delHistory(mCurrentClickHistoryItem);
                                } else {
                                    mHistoryPresenter.clearHistoriesClean();
                                }
                            }
                        }
                    }
                });

                dialog.show();
            }
        });
    }

    private void initPresenter() {
        mHistoryPresenter = HistoryPresenter.getInstance();
        mHistoryPresenter.registerViewCallback(this);

        // 获取数据
        mHistoryPresenter.listHistories();
    }

    private void initView(ViewGroup container) {

        // 设置布局管理
        mHistoryList.setLayoutManager(new LinearLayoutManager(container.getContext()));

        mHistoryList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                outRect.left = UIUtil.dip2px(view.getContext(), 5);
                outRect.right = UIUtil.dip2px(view.getContext(), 5);
            }
        });

    }

    @Override
    public void onHistoriesLoaded(List<Track> tracks) {
        if (tracks != null) {
            mTrackListAdapter.setData(tracks);

            if (tracks.size() == 0) {
                mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
            } else {
                mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
            }
        } else {
            mUiLoader.updateStatus(UILoader.UIStatus.ERROR);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mHistoryPresenter != null) {
            mHistoryPresenter.unRegisterViewCallback(this);
        }

        if (mTrackListAdapter != null) {
            mTrackListAdapter.setItemClickListener(null);
        }
    }

}